package com.itheima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.model.common.dtos.ResponseResult;
import com.itheima.model.common.enums.AppHttpCodeEnum;
import com.itheima.model.media.pojos.WmUser;
import com.itheima.wemedia.mapper.WmUserMapper;
import com.itheima.wemedia.service.WmUserService;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements WmUserService {

    @Override
    public ResponseResult insert(WmUser wmUser) {
        if (wmUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        List<WmUser> wmUsers = list();
        for (WmUser user : wmUsers) {
            if (user.getPhone().equals(wmUser.getPhone())){
                return ResponseResult.errorResult(AppHttpCodeEnum.PHONEEXIST);
            }
        }
        wmUser.setCreatedTime(new Date());
        save(wmUser);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult findByName(String name) {
        if (name==null||name.length()>20){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //先查询有没有对应的名字,没有则返回提示
        LambdaQueryWrapper<WmUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WmUser::getName,name);
        List<WmUser> list = list(lambdaQueryWrapper);

        if (list.size() ==0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }else {
            return ResponseResult.okResult(list.get(0));
        }

    }
}
